﻿package game.network.api {
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import game.network.INetwork;
	import game.objects.GameObject;
	import game.Config;
	import game.objects.UserApiObject;
	import game.debug.Debug;
	
	public class fbDesktop extends GameObject implements INetwork {
		
		public static const FIELDS:String				= "id,first_name,last_name,username,link,gender,picture.type(large)";
		
		public static const GET_FRIENDLIST:String		= "me/friendlists";
		public static const GET_ME:String				= "me/";
		public static const GET_PROFILES:String			= "";
		
		public function getProfiles(uids:String, callBack:Function):void {
			if (uids==null) {
				getMe(callBack);
				return;
			}
			send(fbDesktop.GET_PROFILES, {ids: uids, fields: fbDesktop.FIELDS}, function (objects:Object) { 
				var v:Vector.<UserApiObject> = new Vector.<UserApiObject>;
				for each (var obj:Object in objects) { 
					v.push( new UserApiObject ( obj.id, obj.first_name, obj.last_name, obj.username, obj.picture.data.url, genderToSex(obj.gender), obj.link ) );
				}
				callBack(v);
			});
		}
		
		private function getMe(callBack:Function):void {
			send(fbDesktop.GET_ME, {fields: fbDesktop.FIELDS}, function (obj:Object) { 
				var v:Vector.<UserApiObject> = new Vector.<UserApiObject>;
				v.push( new UserApiObject ( obj.id, obj.first_name, obj.last_name, obj.username, obj.picture.data.url, genderToSex(obj.gender), obj.link ) );
				callBack(v);
			});
		}
		
		public function getFriends(callBack:Function):void {
			send(fbDesktop.GET_FRIENDLIST, {fields: fbDesktop.FIELDS}, function (obj:Object) { 
				var v:Vector.<UserApiObject> = new Vector.<UserApiObject>;
				v.push( new UserApiObject ( obj.id, obj.first_name, obj.last_name, obj.username, obj.picture.data.url, genderToSex(obj.gender), obj.link ) );
				callBack(v);
			});
		}
		
		private function genderToSex(value:String):int {
			var sex:int = 0;
			if (value=='male') sex = 2;
			else if (value=='female') sex = 1;
			else sex = 0;
			return sex;
		}
		
		private function send(method:String, data:Object = null, callBack:Function = null):void {
			
			var vars:URLVariables = new URLVariables;
			for (var i in data) {
				vars[i] = data[i];
			}
			
			vars.access_token = config.me.access_token;
			
			var req:URLRequest	= new URLRequest (Config.FB_API+method);
			req.method			= URLRequestMethod.GET;
			req.data			= vars;
			
			var loader:URLLoader = new URLLoader;
			loader.addEventListener(IOErrorEvent.IO_ERROR, function (event:IOErrorEvent) { errorCallback({error_msg: event.text}); });
			loader.addEventListener(Event.COMPLETE, function (event:Event) { var result:Object = JSON.parse(loader.data); if (result.error) errorCallback ({error_msg: result.error.message}) else callBack(result); });
			loader.load( req );
			
		}
		
		private function errorCallback(error:Object):void {
			debug ( error.error_msg, Debug.API );
		}
	}
}